package com.fingard.dsp.bank.directbank.cib03;

import com.alibaba.fastjson.JSONObject;
import com.fingard.common.utils.json.JSONUtils;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.text.StringHelper;

import java.util.Date;
import java.util.HashMap;

public class CIB03Contract extends CIB03Base implements IContract {
    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem != null) {
                HashMap<String, String> postMapParams = new HashMap<String, String>();
                postMapParams.put("version", "1.0.2");
                postMapParams.put("mchtId", actItem.merchantId);
                postMapParams.put("signType", "RSA");
                postMapParams.put("serialNo", p_req.contractDetail.askNumber);
                postMapParams.put("transTime", Format.DateTime14Format.format(new Date()));
                if ("01".equals(p_req.contractDetail.step)) {
                    postMapParams.put("acctNo", encryptByCFCA(actItem, p_req.contractDetail.payAct));
                    postMapParams.put("acctType", "");
                    postMapParams.put("acctName", encryptByCFCA(actItem,p_req.contractDetail.payName));
                    postMapParams.put("certType", "0");
                    postMapParams.put("certNo", encryptByCFCA(actItem, p_req.contractDetail.certNum));
                    postMapParams.put("rsrvPhoneNo", encryptByCFCA(actItem, p_req.contractDetail.cellPhone));
                    postMapParams.put("cvn", "");
                    postMapParams.put("pubOrPriFlag", p_req.contractDetail.isPrivate);
                } else if ("02".equals(p_req.contractDetail.step)) {
                    postMapParams.put("phoneNo", encryptByCFCA(actItem, p_req.contractDetail.cellPhone));
                    postMapParams.put("smsCode", p_req.contractDetail.smsCode);
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "未能识别的step：" + p_req.contractDetail.step;
                    retAts.writeRetDetail(tmpRetDetail);
                    return retAts;
                }
                WriteBankLogLn("原始待签名数据：" + postMapParams.toString());
                postMapParams.put("mac", generateMAC(postMapParams, actItem));
                String submitUrl = "";
                if ("01".equals(p_req.contractDetail.step)) {
                    submitUrl = "agreementSign";
                } else if ("02".equals(p_req.contractDetail.step)) {
                    submitUrl = "smsCheck";
                }
                String tmpStrRet = sendDataToBank(JSONUtils.map2JSON(postMapParams), submitUrl);
                if (StringHelper.hasAnyChar(tmpStrRet)) {
                    JSONObject tmpJsonRet = JSONObject.parseObject(tmpStrRet);
                    String retCode = tmpJsonRet.getString("respCode");
                    if ("E0000".equals(retCode)) {
                        if ("01".equals(p_req.contractDetail.step)) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpJsonRet.getString("respMsg");
                        } else if ("02".equals(p_req.contractDetail.step)) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpJsonRet.getString("respMsg");
                            tmpRetDetail.signState = ContractState.SUCCESS;
                        }
                    } else {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpJsonRet.getString("respMsg");
                        tmpRetDetail.signState = ContractState.FAIL;
                    }
                    tmpRetDetail.signConfirmType = "2";
                    tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", retCode);
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行响应为空";
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_req.contractDetail.recAct + "]商户配置信息!";
            }
        } catch (Exception e) {
            WriteBankLogLn("");
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.signState = ContractState.UNSIGNED;

        tmpRetDetail.respCode = "0";
        tmpRetDetail.respInfo = "查无此记录";
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}
